package com.tour.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.jdbc.StringUtils;
import com.tour.config.ResponseWrapper;
import com.tour.entity.Medical;
import com.tour.mapper.MedicalMapper;
import com.tour.service.MedicalService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.xmlbeans.impl.jam.mutable.MMember;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lel
 * @since 2021-04-10
 */
@Service
public class MedicalServiceImpl extends ServiceImpl<MedicalMapper, Medical> implements MedicalService {

    @Autowired
    MedicalMapper medicalMapper;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public ResponseWrapper addMedial(Medical medical){
      if (medical != null){
          int result = medicalMapper.insert(medical);
          if ( result == 1){
              return ResponseWrapper.markSuccess(null);
          }else {
              return ResponseWrapper.markError();
          }
      }else {
          return ResponseWrapper.markError();
      }
    }

    public ResponseWrapper deleteMedical(Integer mid){
        if (mid == null){
            return ResponseWrapper.markError();
        }else {
            int result = medicalMapper.deleteById(mid);
            if ( result == 1){
                return ResponseWrapper.markSuccess(null);
            }else {
                return ResponseWrapper.markError();
            }
        }
    }

    public ResponseWrapper updateMedical(Medical medical){
        if (medical != null){
            int result = medicalMapper.updateById(medical);
            if ( result == 1){
                return ResponseWrapper.markSuccess(null);
            }else {
                return ResponseWrapper.markError();
            }
        }else {
            return ResponseWrapper.markError();
        }
    }

    public ResponseWrapper findOneMedical(Integer mid){
        if (mid == null){
            return ResponseWrapper.markError();
        }else {
            Medical medical = medicalMapper.selectById(mid);
            Integer reading = medical.getReading();
            String read = String.valueOf(reading);
            String ScenicKey = String.valueOf("medical"+mid);
            redisTemplate.opsForValue().set(ScenicKey,read);
            stringRedisTemplate.opsForValue().increment(ScenicKey,1);
            int count = Integer.parseInt(stringRedisTemplate.boundValueOps(ScenicKey).get(0,-1));
            medical.setReading(count);
            medicalMapper.updateById(medical);
            ResponseWrapper wrapper = ResponseWrapper.markSuccess(medical);
            wrapper.getData().put("data",medical);
            return wrapper;
        }
    }
    public ResponseWrapper findMedeical(String name,Integer pageSize,Integer pageNum){
        QueryWrapper<Medical> medicalQueryWrapper = new QueryWrapper<>();
        medicalQueryWrapper.like(!StringUtils.isNullOrEmpty(name),"medical_name",name).orderByDesc("reading");
        Page<Medical> page = new Page<>(pageNum,pageSize);
        IPage<Medical> iPage = medicalMapper.selectPage(page,medicalQueryWrapper);
        List<Medical> medicalList = iPage.getRecords();
        ResponseWrapper wrapper = ResponseWrapper.markSuccess(medicalList);
        wrapper.getData().put("data",medicalList);
        wrapper.getData().put("total",page.getTotal());
        wrapper.getData().put("pageNum",page.getCurrent());
        wrapper.getData().put("pageSize",page.getSize());
        return wrapper;
    }

}
